package com.candy.test;

import com.candy.bean.Blog;
import com.candy.bean.StudentInfo;
import com.candy.dao.BlogMapper;
import com.candy.dao.StudentInfoMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class test01 {
    public static void main(String[] args) {
        test05();

    }
    public static void test05() {
        try{
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper=session.getMapper(StudentInfoMapper.class);
              StudentInfo studentInfo=new StudentInfo();
              studentInfo.setStuAddress("青岛");
              studentInfo.setStuName("战豆豆");
              studentInfo.setStuPwd("9876");
              studentInfo.setStuTel("17909876543");
           int num=mapper.addStudent(studentInfo);
            System.out.println(num);
            session.commit();
        }catch(Exception e){
            System.out.println(e);


        }

    }


    public static void test04() {
        try{
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper=session.getMapper(StudentInfoMapper.class);
          List<StudentInfo> ls= mapper.selectAllMsg();
            for (StudentInfo u:ls
                 ) {
                System.out.println(u);
            }
        }catch(Exception e){
            System.out.println(e);


        }

    }


    public static void test03() {
        try{
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper=session.getMapper(StudentInfoMapper.class);
            StudentInfo b=mapper.selectStudentInfoById(1);
            System.out.println(b);
        }catch(Exception e){
            System.out.println(e);


        }

    }
    public static void test02() {
        try{
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            BlogMapper mapper=session.getMapper(BlogMapper.class);
            Blog b=mapper.selectBlog(1);
            System.out.println(b);
        }catch(Exception e){
            System.out.println(e);


        }

    }

    public static void test01() {
        try{
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlogs", 1);
            System.out.println(blog);
        }catch(Exception e){
            System.out.println(e);


        }
    }
}
